package com.clwl.supervise.superviseapp.controller.sampling;

import com.clwl.supervise.superviseapp.entity.request.AddSamplingCompany;
import com.clwl.supervise.superviseapp.entity.request.PushCompanyHistory;
import com.clwl.supervise.superviseapp.entity.request.SamplingCompany;
import com.clwl.supervise.superviseapp.entity.result.ResultEntity;
import com.clwl.supervise.superviseapp.entity.risk.JgStaticScoring;
import com.clwl.supervise.superviseapp.entity.supervise.JgSamplingCompany;
import com.clwl.supervise.superviseapp.entity.vo.JgSamplingStatistics;
import com.clwl.supervise.superviseapp.entity.vo.TcCompanyVo;
import com.clwl.supervise.superviseapp.service.sampling.JgSamplingService;
import com.clwl.supervise.superviseapp.util.file.FileResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 抽检监管模块
 */
@RestController
@RequestMapping("/sampling")
public class JgSamplingController {
    @Autowired
    private JgSamplingService samplingService;

    /**
     * 查看企业列表(分页)
     *
     * @param company
     * @return
     */
    @GetMapping("querySamplingCompanyList")
    public ResultEntity<List<JgSamplingCompany>> querySamplingCompanyList(SamplingCompany company) {
        company.setSupProcess("spsc");
        List<JgSamplingCompany> dataList = samplingService.querySamplingCompanyList(company);
        return ResultEntity.success(dataList);
    }

    /**
     * 查询企业分页历史列表
     *
     * @param history
     * @return
     */
    @GetMapping("queryPushHistoryList")
    public ResultEntity<List<TcCompanyVo>> queryPagePushHistoryList(PushCompanyHistory history) {
        history.setSupProcess("spsc");
        List<TcCompanyVo> dataList = samplingService.queryPagePushHistoryList(history);
        return ResultEntity.success(dataList);
    }

    /**
     * 提交企业信息认领
     *
     * @param pushId 企业推送id
     * @return
     */
    @PostMapping(value = "submitClaimStatus")
    @ResponseBody
    public ResultEntity<String> submitClaimStatus(Long pushId) {
        try {
            boolean result = samplingService.updateClaimStatus(pushId, "1");
            return result == true ? ResultEntity.success("操作成功") : ResultEntity.error("操作失败!");
        } catch (Exception e) {
            return ResultEntity.error("操作失败!");
        }
    }

    /**
     * 退回认领接口
     *
     * @param pushId 企业推送id
     * @return
     */
    @PostMapping(value = "cancelClaim")
    @ResponseBody
    public ResultEntity<String> cancelClaim(Long pushId) {
        try {
            boolean result = samplingService.updateClaimStatus(pushId, "2");
            return result == true ? ResultEntity.success("操作成功") : ResultEntity.error("操作失败!");
        } catch (Exception e) {
            return ResultEntity.error("操作失败!");
        }
    }

    /**
     * 企业推送回显接口
     *
     * @param pushId
     * @return
     */
    @GetMapping("openSamplingView")
    public ResultEntity<TcCompanyVo> openSamplingView(Long pushId) {
        try {
            TcCompanyVo tcCompanyVo = samplingService.queryPushCompany(pushId);
            return ResultEntity.success(tcCompanyVo);
        } catch (Exception e) {
            return ResultEntity.error("操作失败!");
        }
    }

    /**
     * 统计查询
     *
     * @param company
     * @return
     */
    @GetMapping("querySamplingStatistics")
    public ResultEntity<JgSamplingStatistics> querySamplingStatistics(SamplingCompany company) {
        try {
            company.setSupProcess("spsc");
            JgSamplingStatistics samplingStatistics = samplingService.querySamplingStatistics(company);
            return ResultEntity.success(samplingStatistics);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("操作失败!");
        }
    }

    /**
     * 上传抽样确认单接口
     *
     * @param file 上传确认单图片
     * @return
     */
    @PostMapping("uploadSamplingFile")
    public ResultEntity<FileResultEntity> uploadSamplingFile(String file) {
        try {
            FileResultEntity fileResultEntity = samplingService.uploadSamplingFile(file);
            return ResultEntity.success(fileResultEntity);
        } catch (Exception e) {
            return ResultEntity.error("操作失败");
        }
    }

    /**
     * 保存确认单信息
     *
     * @param company
     * @return
     */
    @PostMapping("saveSamplingCompany")
    public ResultEntity saveSamplingCompany(AddSamplingCompany company) {
        try {
            samplingService.saveSamplingCompanyInfo(company);
            return ResultEntity.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("操作失败");
        }
    }
}
